package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallBrand;
import com.ctsi.ssdc.domain.LitemallBrandExample;
import com.ctsi.ssdc.service.LitemallBrandService;
import com.ctsi.ssdc.repository.LitemallBrandRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

/**
 * Service Implementation for managing LitemallBrand.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallBrandServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallBrandRepository, LitemallBrand, Integer, LitemallBrandExample> 
	implements LitemallBrandService {




    @Autowired
    LitemallBrandRepository litemallBrandRepository;
    private LitemallBrand.Column[] columns = new LitemallBrand.Column[]{LitemallBrand.Column.id, LitemallBrand.Column.name, LitemallBrand.Column.desc, LitemallBrand.Column.picUrl, LitemallBrand.Column.floorPrice};


    /**
    * GET  /litemallBrands : get the litemallBrands firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallBrand> findFirstStringColumn(String name,Pageable pageable){
        String str = name;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallBrandExample litemallBrandExample = new LitemallBrandExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallBrandExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallBrandExample.createCriteria();
        } else{
            litemallBrandExample.createCriteria().andNameLike("%" + str +"%");
        }
        List<LitemallBrand>  data = litemallBrandRepository.selectByExample(litemallBrandExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallBrandRepository.countByExample(litemallBrandExample);
        }
        return new PageResult<LitemallBrand>(data,count,count);
    }

    @Override
    public List<LitemallBrand> query(Integer page, Integer limit, String sort, String order) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false);
        if (!org.springframework.util.StringUtils.isEmpty(sort) && !org.springframework.util.StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return litemallBrandRepository.selectByExampleSelective(example, columns);
    }

    @Override
    public LitemallBrand findById(Integer id) {
        return litemallBrandRepository.selectByPrimaryKey(id);
    }

    @Override
    public List<LitemallBrand> query(Integer page, Integer limit) {
        return query(page, limit, null, null);
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
